package com.taxi.driver.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.taxi.driver.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 考核统计里的时间切换控件
 */
public class TimeTable extends RelativeLayout implements View.OnClickListener{
    private RelativeLayout rl_left,rl_right;
//    ImageView image_right;
    private TextView tv_date;
    private String startDate,endDate;
    private DateListener listener;
    private String currentDay;
    private String paramterEndData;//传的参数结束时间 (请求的数据是从传的结束的时间前一天算起,所以显示的是前一天的时间,请求的时候传的参数还是今天的时间)
    public TimeTable(Context context) {
        this(context, null);
    }

    public TimeTable(Context context, AttributeSet attrs) {
        this(context, attrs ,0);
    }

    public TimeTable(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView(){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_time_table,null);
//        image_right= (ImageView) view.findViewById(R.id.image_right);
        rl_left = (RelativeLayout) view.findViewById(R.id.rl_left);
        rl_right = (RelativeLayout)view.findViewById(R.id.rl_right);
        tv_date = (TextView) view.findViewById(R.id.tv_date);
        rl_left.setOnClickListener(this);
        rl_right.setOnClickListener(this);
        addView(view);

        startDate = getMonday(new Date());
        endDate = getAddDate(startDate,6);
        paramterEndData=endDate;
        detailCurrentTime();
        currentDay=endDate;
        tv_date.setText(startDate+getResources().getString(R.string.to)+endDate);

        if (Integer.valueOf(getAddDate(endDate,1).replace("-",""))>Integer.valueOf(currentDay.replace("-",""))){
            rl_right.setVisibility(View.GONE);
            rl_right.setClickable(false);
        }
    }

    /**
     * 处理结束时间显示的问题
     */
    private void detailCurrentTime() {
        if (str2Date(endDate).getTime()>new Date().getTime()) {
            endDate=getAddDate(date2Str(new Date()),-1);
            if(str2Date(endDate).getTime()<str2Date(startDate).getTime()) {
                //跳转到上一周
                subWeek();
            }
        }
    }

    /**
     * 获得指定日期所在周的星期一的日期
     * @param date
     */
    private String getMonday(Date date){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int add = 0;
        switch (c.get(Calendar.DAY_OF_WEEK)) {
            case 1:
                add = -6;
                break;
            case 2:
                add = 0;
                break;
            case 3:
                add = -1;
                break;
            case 4:
                add = -2;
                break;
            case 5:
                add = -3;
                break;
            case 6:
                add = -4;
                break;
            case 7:
                add = -5;
                break;
        }
        c.add(Calendar.DAY_OF_MONTH,add);
        return date2Str(c.getTime());
    }

    /**
     * 获得开始时间
     * @return
     */
    public String getStartDate(){
        return startDate;
    }

    /**
     * 传给后天的结束时间参数
     * 获得结束时间
     * @return
     */
    public String getEndDate(){
        return paramterEndData;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_left:
                subWeek();
                break;
            case R.id.rl_right:
                startDate = getAddDate(endDate,1);
                endDate = getAddDate(startDate,6);
                //处理当前的时间显示问题
                detailCurrentTime();
                if (Integer.valueOf(getAddDate(endDate,1).replace("-",""))>Integer.valueOf(currentDay.replace("-",""))){
                    rl_right.setVisibility(View.GONE);
                    rl_right.setClickable(false);

                    tv_date.setText(startDate+getResources().getString(R.string.to)+endDate);
                }else{
                    rl_right.setVisibility(View.VISIBLE);
                    rl_right.setClickable(true);
//                    startDate = getAddDate(endDate,1);
//                    endDate = getAddDate(startDate,6);
                    tv_date.setText(startDate+getResources().getString(R.string.to)+endDate);
                }
                addWeek();
                break;
        }

        if(listener!=null)
            listener.onDate(startDate,endDate);
    }

    /**
     * Date转日期字符串
     * @return
     */
    private String date2Str(Date date){
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(date);
    }

    /**
     * 时间字符串转Date
     * @param str
     * @return
     */
    private Date str2Date(String str) {
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获得指定日期增加add天后的日期
     * @param startDate
     * @return
     */
    private String getAddDate(String startDate,int add){
        Date date = str2Date(startDate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH,add);
        return date2Str(c.getTime());
    }

    /**
     * 切换下一周日期
     */
    private void addWeek(){

//        KLog.e("============currentStartDay======="+startDate);

//        if (Integer.valueOf(startDate.replace("-",""))>Integer.valueOf(currentDay.replace("-",""))){
////            ToastUtil.getInstance().toast("下周大于这周");
//            rl_right.setVisibility(View.GONE);
//            rl_right.setClickable(false);
//            return;
//        }



    }

    /**
     * 切换上一周日期
     */
    private void subWeek(){
        rl_right.setVisibility(View.VISIBLE);
        rl_right.setClickable(true);
        endDate = getAddDate(startDate,-1);
        startDate = getAddDate(endDate,-6);
        tv_date.setText(startDate+getResources().getString(R.string.to)+endDate);
    }

    public interface DateListener{
        void onDate(String startDate,String endDate);
    }

    public void setDateListener(DateListener listener){
        this.listener=listener;
    }
}
